package cn.xsly.org.controller;

import cn.xsly.system.annotation.PreAuthorize;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.basic.util.PageList;
import cn.xsly.org.domain.Department;
import cn.xsly.org.query.DepartmentQuery;
import cn.xsly.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询单个接口
     * @param id
     * @return
     */
    @ApiOperation("通过Id查询")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个部门", sn = "department:getOne")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperation("查询所有")
    @GetMapping
    @PreAuthorize(name = "查询所有部门", sn = "department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }


    /**
     * 分页查询+高级查询接口
     * @param query
     * @return
     */
    @ApiOperation("分页查询加高级查询")
    @PostMapping
    @PreAuthorize(name="部门列表",sn="department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }


    /**
     * 添加和保存的接口
     * @param department
     * @return
     */
    @ApiOperation(value = "新增或修改")
    @PutMapping
    @PreAuthorize(name="部门保存",sn="department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }


    /**
     * 删除单个对象接口
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name="部门删除",sn="department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }


    /**
     * 批量删除对象接口
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据id批量删除")
    @PatchMapping
    @PreAuthorize(name="部门批量删除",sn="department:patchDel")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }


    @ApiOperation(value = "无限级树")
    @GetMapping("/deptTree")
    @PreAuthorize(name="部门树",sn="department:tree")
    public List<Department> queryTree(){
           return departmentService.queryTree();
    }


}
